import java.util.Arrays;

/**
 * Created with Interllij IDEA.
 * Descriptiption:
 * User: 王澄慧
 * Date: 2022-04-03
 * Time: 15:37
 */
public class FristDemo {
    /**
     * 不规则数组
     * @param args
     */
    public static void main(String[] args) {
        int[][] array = new int[2][];
        array[0] = new int[2];
        array[1] = new int[4];
        for (int i = 0; i < array.length; i++) { // 行数
            for (int j = 0; j < array[i].length; j++) { //列数
                System.out.print(array[i][j] + " ");
            }
            System.out.println();
        }
    }
    public static void main8(String[] args) {
        int[][] array ={{2,3,4,5},{6,7,8,9}};
        for (int i = 0; i < array.length; i++) { // 行数
            for (int j = 0; j < array[i].length; j++) { //列数
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
        //用foreach打印
        System.out.println("用foreach打印");
        for (int[] tem:array) {
            for (int x:tem) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        System.out.println("用toSting打印:");
        System.out.println(Arrays.deepToString(array));
    }

    /**
     * 逆置数组
     * @param
     */
    public static void fun3(int[] array1){
        int left = 0;
        int right = array1.length-1;
        int tem = 0;
        while (left < right){
            tem = array1[left];
            array1[left] = array1[right];
            array1[right] = tem;
            left++;
            right--;
        }
    }
    public static void main7(String[] args) {
        int[] array1 = {12,31,45,67,11};
        System.out.println("逆置之前的数组"+Arrays.toString(array1));
        fun3(array1);
        System.out.println("逆置之后的数组"+Arrays.toString(array1));
    }

    /**
     * 冒泡排序
     * @param array
     */
    public static void fun(int[] array){
        for (int i = 0; i < array.length-1; i++) { //1.i是躺数
            boolean fig = false; //不能放在for循环外面
            for (int j = 0; j < array.length-1-i; j++) { //可以不减i，减i表示优化
                if(array[j] > array[j+1]){
                    int tem = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tem;
                    fig = true;
                }
            }
            if(fig == false){
                break;
            }
        }
    }
    public static void main6(String[] args) {
        int[] array = {12,23,21,13,15};
        int[] array1 = {12,14,13,15,16};
        System.out.println("排列之前的数组"+Arrays.toString(array));
        fun(array);
        System.out.println("排列之后的数组"+Arrays.toString(array));
        System.out.println("排列之前的数组"+Arrays.toString(array1));
        fun(array1);
        System.out.println("排列之后的数组"+Arrays.toString(array1));
    }
    /**
     * Arrays.equals和Arrays.fill的使用
     * @param args
     */
    public static void main5(String[] args) {
        int[] array1 = {1,2,3,4,5};
        int[] array2 = {12,3,4,12,1};
        int[] array3 = {1,2,3,4,5};
        Arrays.equals(array1,array2);
        Arrays.equals(array1,array3);
        boolean flg = Arrays.equals(array1,array2);
        boolean tem = Arrays.equals(array1,array3);
        System.out.println(flg);
        System.out.println(tem);
        System.out.println("=============");
        int[] array = new int[20];
        System.out.println("填充之前的数组"+Arrays.toString(array));
        Arrays.fill(array,0,13,24);
        Arrays.fill(array,13,array.length,13);
        System.out.println("填充之前的数组"+Arrays.toString(array));

    }
    public static void main4(String[] args) {
        int[] array ={23,34,45,56,24};
        Arrays.sort(array); //sort默认为升序排序
        System.out.println("；排列之后的数组"+Arrays.toString(array));
        int tem = Arrays.binarySearch(array,24); //二分法Arrays.binarySearch，找不到最后一次low的位置+1
        if(tem == -1){
            System.out.println("没有找到你要的关键字：");
        }else {
            System.out.println("找到了你要的关键字：" + tem);
        }
    }
    /**
     * 二分法
     * @param array
     * @param key
     * @return
     */
    public static int fun1(int[] array,int key) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int mid = (left + right) / 2;
                if (array[mid] < key) {
                    left = mid + 1;
                } else if (array[mid] == key) {
                    return mid;
                }else{
                    right = mid -1;
            }
        }
        return -1;
    }

    public static void main3(String[] args) {
        int[] array ={23,34,45,56,24};
        Arrays.sort(array); //sort默认为升序排序
        System.out.println("排列之后的数组"+Arrays.toString(array));
        /*int tem = fun(array,24);
        if(tem == -1){
            System.out.println("没有找到你要的关键字：");
        }else {
            System.out.println("找到了你要的关键字：" + tem);
        }*/
    }

    /**
     * 找到你想要的数组的关键字的下标
     * @param array
     * @param key
     * @return
     */
    public static int fun(int[] array,int key){
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key){
                return i;
            }
        }
        return -1;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};
        int tem = fun(array,4);
        if(tem == -1){
            System.out.println("没有找到你要的关键字：");
        }else {
            System.out.println("找到了你要的关键字：" + tem);
        }
    }

    /**
     * 求一个数组的平均数
     * @param array
     * @return
     */
    public static double avg(int[] array){
        int sum = 0;
        for (int i = 0; i < array.length ; i++) {
            sum = sum+array[i];
        }
        double tem = sum*1.0/array.length;
        return tem;
    }
    public static void main1(String[] args) {
        int[] array1 = {2,3,4,5,6};
        System.out.println(avg(array1));
    }
}
